﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rhino.Mocks;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace KandaAlpha.Application.Impl.Tests
{
    public abstract class BaseTest
    {
        protected readonly MockRepository _mocks;
        public BaseTest()
        {
            _mocks = new MockRepository(); 
        }

        protected IEnumerable<System.Reflection.PropertyInfo> GetPropertiesFrom<T>(T type)
        {
            return type.GetType().GetProperties().Where(p => p.PropertyType.FullName.StartsWith("System") && p.CanWrite && !p.PropertyType.IsArray);
        }
        
        /// <summary>
        /// Creates a Fake object and populates with fake values. 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        protected T CreateFake<T>()
        {
            var fake = Activator.CreateInstance<T>();
            var systemProperties = GetPropertiesFrom(fake); 
            foreach (var pi in systemProperties)
            {
                if (pi.PropertyType == typeof(int))
                {
                    pi.SetValue(fake, 1, null); 
                    continue;
                }

                if (pi.PropertyType == typeof(string)) 
                {
                    pi.SetValue(fake,pi.Name + "Value", null);
                    continue;
                }

                if (pi.PropertyType == typeof(DateTime)) 
                {
                    pi.SetValue(fake, new DateTime(1999, 12, 31), null); 
                    continue;
                }
            }

            return fake; 
        }

        /// <summary>
        /// Iterates through the supplied fake object and runs Assertions on the property values
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fake"></param>
        protected void VerifyFake<T>(T fake)
        {
            var systemProperties = GetPropertiesFrom(fake); 
            foreach (var pi in systemProperties)
            {
                if (pi.PropertyType == typeof(int))
                {
                    Assert.AreEqual(1, pi.GetValue(fake, null)); 
                    continue; 
                }

                if (pi.PropertyType == typeof(string)) 
                {
                    Assert.AreEqual(pi.Name + "Value", pi.GetValue(fake, null)); 
                    continue; 
                }

                if (pi.PropertyType == typeof(DateTime)) 
                {
                    Assert.AreEqual(new DateTime(1999, 12, 31), pi.GetValue(fake, null)); 
                    continue; 
                }
            }

        }
    }
}
